import { PlusOutlined } from '@ant-design/icons';
import { Button } from 'antd';
import { history } from 'umi';
import React, { useRef } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { list } from './servic';
import type { TableListItem, TableListPagination } from './data';

const TableList: React.FC = () => {
  const actionRef = useRef<ActionType>();
  /** 国际化配置 */

  const columns: ProColumns<TableListItem>[] = [
    {
      title: '就诊人id',
      dataIndex: 'visitorId',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '就诊人',
      dataIndex: 'visitorName',
    },
    {
      title: '就诊日期',
      dataIndex: 'visitDate',
    },
    {
      title: '就诊医院',
      dataIndex: 'hospitalName',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '就诊科室',
      dataIndex: 'deptName',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '诊断结果',
      dataIndex: 'diagnosticResult',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '治疗方案',
      dataIndex: 'treatmentPlan',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '治疗效果',
      dataIndex: 'effect',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '是否已关联案例文章',
      dataIndex: 'articleId',
      hideInForm: true,
      hideInSearch: true,

      render: (text, _, index) => {
        if (text != null) {
          return '有';
        } else return '无';
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="eidt"
          onClick={() => {
            history.push(
              '/case/records-detail?type=1&id=' + record.id + '&caseId=' + record.caseId,
            );
          }}
        >
          编辑
        </a>,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<TableListItem, TableListPagination>
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        headerTitle={
          <Button
            type="primary"
            key="primary"
            onClick={() => {
              history.push('/case/records-detail?type=1');
            }}
          >
            <PlusOutlined /> 新增
          </Button>
        }
        request={async (
          // 第一个参数 params 查询表单和 params 参数的结合
          // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
          params,
          sorter,
          filter,
        ) => {
          // console.log(params, sorter, filter);
          // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
          // 如果需要转化参数可以在这里进行修改
          const msg = await list({
            pageNum: params.current,
            pageSize: params.pageSize,
          });
          console.log('msg', msg);
          if (msg.code == 200) {
            return {
              data: msg.data.list,
              // success 请返回 true，
              // 不然 table 会停止解析数据，即使有数据
              success: msg.message,
              // 不传会使用 data 的长度，如果是分页一定要传
              total: msg.data.total,
            };
          } else {
          }
        }}
        columns={columns}
      />
    </PageContainer>
  );
};

export default TableList;
